home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / vbof_v11 / demoprsn.cls < prev    next >
Encoding:
Text File  |  1996-03-01  |  17.8 KB  |  647 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Person"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = True
  8. Option Explicit
  9.  
  10. ' the following pertain to being supported by
  11. '   VBOFCollection, VBOFObjectManager and
  12. '   VBOFEventManager
  13. Public ObjectID As Long
  14. Public ObjectChanged As Long
  15. Public ObjectAdded As Long
  16. Public ObjectDeleted As Long
  17. Public ObjectParentCount As Long
  18. Public ObjectManager As VBOFObjectManager
  19.  
  20. ' the following code pertains to the business
  21. '   of the Person object
  22. Private pvtCustomerNumber As Long
  23. Private pvtFirstName As String
  24. Private pvtLastName As String
  25. Private pvtMaidenName As String
  26. Private pvtSSN As Long
  27. Private pvtSex As String
  28. Private pvtDateOfBirth As Variant
  29. Private pvtDateOfDeath As Variant
  30. Private pvtMaritalStatus As String
  31. Private pvtAddressesCollection As VBOFCollection
  32. Private pvtPhonesCollection As VBOFCollection
  33. Private pvtMother As Person
  34. Private pvtFather As Person
  35. Private pvtSpouse As Person
  36.  
  37. Public Function ObjectSortCompare(Optional SortField As Variant, Optional SortOrder As Variant, Optional CompareObject As Variant) As Long
  38. ' Support the Collection.Sort method
  39. ' Note: use the ObjectManager.ObjectSortCompare
  40. '   method for assistance
  41.  
  42.     On Local Error Resume Next
  43.  
  44.     Select Case SortField
  45.         Case "FirstName"
  46.             ObjectSortCompare = _
  47.                 ObjectManager.ObjectSortCompare( _
  48.                     Value1:=FirstName, _
  49.                     Value2:=CompareObject.FirstName, _
  50.                     SortOrder:=SortOrder)
  51.         
  52.         Case "LastName"
  53.             ObjectSortCompare = _
  54.                 ObjectManager.ObjectSortCompare( _
  55.                     Value1:=LastName, _
  56.                     Value2:=CompareObject.LastName, _
  57.                     SortOrder:=SortOrder)
  58.         
  59.         Case "FormattedName"
  60.             ObjectSortCompare = _
  61.                 ObjectManager.ObjectSortCompare( _
  62.                     Value1:=FormattedName, _
  63.                     Value2:=CompareObject.FormattedName, _
  64.                     SortOrder:=SortOrder)
  65.         
  66.         Case "DateOfBirth"
  67.             ObjectSortCompare = _
  68.                 ObjectManager.ObjectSortCompare( _
  69.                     Value1:=Format$(DateOfBirth, "yyyy/mm/dd"), _
  70.                     Value2:=Format$(CompareObject.DateOfBirth, "yyyy/mm/dd"), _
  71.                     SortOrder:=SortOrder)
  72.         
  73.         Case "DateOfDeath"
  74.             ObjectSortCompare = _
  75.                 ObjectManager.ObjectSortCompare( _
  76.                     Value1:=Format$(DateOfDeath, "yyyy/mm/dd"), _
  77.                     Value2:=Format$(CompareObject.DateOfDeath, "yyyy/mm/dd"), _
  78.                     SortOrder:=SortOrder)
  79.         
  80.         Case "SpouseFormattedName"
  81.             ObjectSortCompare = _
  82.                 ObjectManager.ObjectSortCompare( _
  83.                     Value1:=Spouse.FormattedName, _
  84.                     Value2:=CompareObject.Spouse.FormattedName, _
  85.                     SortOrder:=SortOrder)
  86.     End Select
  87.  
  88. End Function
  89.  
  90.  
  91. Public Property Set Father(aPerson As Variant)
  92.     Set pvtFather = aPerson
  93. End Property
  94.  
  95. Public Property Set Spouse(aPerson As Variant)
  96.     Set pvtSpouse = aPerson
  97. End Property
  98.  
  99.  
  100. Public Property Get Father() As Variant
  101.     Set Father = pvtFather
  102. End Property
  103.  
  104. Public Property Get Spouse() As Variant
  105.     Set Spouse = pvtSpouse
  106. End Property
  107.  
  108.  
  109. Public Property Set Mother(aPerson As Variant)
  110.     Set pvtMother = aPerson
  111. End Property
  112.  
  113. Public Property Get Mother() As Variant
  114.     Set Mother = pvtMother
  115. End Property
  116.  
  117.  
  118. Public Function ObjectDBGridUnboundReadData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional RowNumber As Variant) As Boolean
  119. ' Populate the DBGrid RowBuf with values from
  120. '   variables within this object
  121. '   (in support of VBOFCollection)
  122. ' Parameter Description:
  123. '   DBGrid:= the DBGrid which is being
  124. '       populated
  125. '   RowBuf:= the current DBGrid RowBuf object
  126. '   RowNumber:= the row number being processed
  127.  
  128.     Dim I As Long
  129.     
  130.     For I = 0 To RowBuf.ColumnCount - 1
  131.         Select Case RowBuf.ColumnName(I)
  132.             Case "CustomerNumber"
  133.                 RowBuf.Value(RowNumber, I) = CustomerNumber
  134.             Case "FirstName"
  135.                 RowBuf.Value(RowNumber, I) = FirstName
  136.             Case "LastName"
  137.                 RowBuf.Value(RowNumber, I) = LastName
  138.             Case "MaidenName"
  139.                 RowBuf.Value(RowNumber, I) = MaidenName
  140.             Case "SSN"
  141.                 RowBuf.Value(RowNumber, I) = SSN
  142.             Case "Sex"
  143.                 RowBuf.Value(RowNumber, I) = Sex
  144.             Case "DateOfBirth"
  145.                 RowBuf.Value(RowNumber, I) = DateOfBirth
  146.             Case "DateOfDeath"
  147.                 RowBuf.Value(RowNumber, I) = DateOfDeath
  148.             Case "MaritalStatus"
  149.                 RowBuf.Value(RowNumber, I) = MaritalStatus
  150.             Case "ObjectID"
  151.                 RowBuf.Value(RowNumber, I) = ObjectID
  152.         End Select
  153.     Next I
  154.  
  155. End Function
  156. Public Function ObjectDBGridUnboundAddData(Optional DBGrid As Variant, Optional RowBuf As Variant, Optional NewRowBookmark As Variant) As Boolean
  157. ' Populate the object variables with the values
  158. '   provided by the user in the new row of the
  159. '   DBGrid
  160. '   (in support of VBOFCollection)
  161. '
  162. ' Parameter Description:
  163. '   DBGrid:= the DBGrid which is being
  164. '       populated
  165. '   RowBuf:= the current DBGrid RowBuf object
  166. '   NewRowBookmark:= the row number being processed
  167.  
  168.     Dim I As Long
  169.     
  170.     For I = 0 To RowBuf.ColumnCount - 1
  171.         If Not IsNull(RowBuf.Value(0, I)) Then
  172.             Select Case RowBuf.ColumnName(I)
  173.                 Case "CustomerNumber"
  174.                     CustomerNumber = RowBuf.Value(0, I)
  175.                 Case "FirstName"
  176.                     FirstName = RowBuf.Value(0, I)
  177.                 Case "LastName"
  178.                     LastName = RowBuf.Value(0, I)
  179.                 Case "MaidenName"
  180.                     MaidenName = RowBuf.Value(0, I)
  181.                 Case "SSN"
  182.                     SSN = RowBuf.Value(0, I)
  183.                 Case "Sex"
  184.                     Sex = RowBuf.Value(0, I)
  185.                 Case "DateOfBirth"
  186.                     DateOfBirth = RowBuf.Value(0, I)
  187.                 Case "DateOfDeath"
  188.                     DateOfDeath = RowBuf.Value(0, I)
  189.                 Case "MaritalStatus"
  190.                     MaritalStatus = RowBuf.Value(0, I)
  191.     
  192. ' Note:  Do not initialize the ObjectID.
  193.         
  194.             End Select
  195.         End If
  196.     Next I
  197.     
  198. ' return "OK" status
  199.     ObjectDBGridUnboundAddData = True
  200. End Function
  201.  
  202.  
  203. Public Function ObjectListBoxValue() As String
  204. ' Return a String will represent this object
  205. '   in a ListBox
  206. '   (in support of VBOFCollection)
  207.  
  208.     ObjectListBoxValue = _
  209.         FormattedName
  210.  
  211. End Function
  212.  
  213. Public Function PersonalInformation() As String
  214. ' Returns a string containing the formatted names
  215. '   of this person's parents or spouse
  216.  
  217.     Dim ReturnString As String
  218.     
  219.     If pvtMother Is Nothing _
  220.     And pvtFather Is Nothing _
  221.     And pvtSpouse Is Nothing _
  222.     Then
  223.         PersonalInformation = "There is no known lineage for this person"
  224.         Exit Function
  225.     End If
  226.     
  227.     ReturnString = "Personal Info: "
  228.     
  229.     If Not pvtMother Is Nothing Then
  230.         ReturnString = ReturnString & _
  231.             " Mother: " & _
  232.             pvtMother.FormattedName
  233.     End If
  234.  
  235.     If Not pvtFather Is Nothing Then
  236.         ReturnString = ReturnString & _
  237.             " Father: " & _
  238.             pvtFather.FormattedName
  239.     End If
  240.  
  241.     If Not pvtSpouse Is Nothing Then
  242.         ReturnString = ReturnString & _
  243.             " Spouse: " & _
  244.             pvtSpouse.FormattedName
  245.     End If
  246.  
  247.     PersonalInformation = ReturnString
  248. End Function
  249.  
  250. Private Sub Class_Terminate()
  251.     If Not ObjectManager Is Nothing Then
  252.         ObjectManager.TerminateObject _
  253.             Object:=Me
  254.     End If
  255. End Sub
  256.  
  257.  
  258. Public Function ObjectHasChanged()
  259. ' Mark this object as "Changed" and trigger the
  260. '   "Changed" event
  261. ' Note: if processing in SynchronousCommit mode,
  262. '   it might be advisable to execute this method
  263. '   after any of the application's properties have
  264. '   changed.  Otherwise, it might be best to have
  265. '   the application GUI execute this method after
  266. '   posting a series of property changes.
  267.  
  268.     ObjectChanged = True
  269.     
  270. #If NoEventMgr = False Then
  271.     If Not ObjectManager Is Nothing Then
  272.         ObjectManager. _
  273.             TriggerObjectEvent _
  274.                 Event:="Changed", _
  275.                 Object:=Me
  276.     End If
  277. #End If
  278.  
  279. End Function
  280.  
  281. Public Function AddPhone(Optional Item As Variant, Optional Parent As Variant) As Phone
  282. ' Return a Phone, added to my pvtPhonesCollection
  283. '   (just a wrapper method for Phones.Add)
  284.  
  285.     Set AddPhone = Me.Phones.Add( _
  286.         Item:=Item, _
  287.         Parent:=Me)
  288.  
  289.     ObjectHasChanged
  290. End Function
  291.  
  292. Public Function ObjectInitializeFromRecordSet(Optional RecordSet As Variant) As Person
  293. ' Populate my variables from the RecordSet
  294. '   (in support of VBOFCollection)
  295.  
  296.     Dim NewPerson As New Person
  297.  
  298.     On Local Error Resume Next
  299.     
  300.     CustomerNumber = RecordSet("CustomerNumber")
  301.     FirstName = RecordSet("FirstName")
  302.     LastName = RecordSet("LastName")
  303.     SSN = RecordSet("SSN")
  304.     Sex = RecordSet("Sex")
  305.     If IsNull(RecordSet("DateOfBirth")) Then
  306.         DateOfBirth = #1/1/01#
  307.     Else
  308.         DateOfBirth = RecordSet("DateOfBirth")
  309.     End If
  310.     If IsNull(RecordSet("DateOfDeath")) Then
  311.         DateOfDeath = #1/1/01#
  312.     Else
  313.         DateOfDeath = RecordSet("DateOfDeath")
  314.     End If
  315.     MaritalStatus = RecordSet("MaritalStatus")
  316.     MaidenName = RecordSet("MaidenName")
  317.     
  318.     ObjectID = RecordSet("ObjectID")
  319.     
  320. ' pick-up the Mother object
  321.     If Not IsNull(RecordSet("MotherObjectID")) Then
  322.         Set pvtMother = _
  323.             ObjectManager. _
  324.                 NewObject( _
  325.                     Sample:=NewPerson, _
  326.                     ObjectID:=CStr(RecordSet("MotherObjectID")))
  327.     End If
  328.     
  329. ' pick-up the Father object
  330.     If Not IsNull(RecordSet("FatherObjectID")) Then
  331.         Set pvtFather = _
  332.             ObjectManager. _
  333.                 NewObject( _
  334.                     Sample:=NewPerson, _
  335.                     ObjectID:=CStr(RecordSet("FatherObjectID")))
  336.     End If
  337.     
  338. ' pick-up the Spouse object
  339.     If Not IsNull(RecordSet("SpouseObjectID")) Then
  340.         Set pvtSpouse = _
  341.             ObjectManager. _
  342.                 NewObject( _
  343.                     Sample:=NewPerson, _
  344.                     ObjectID:=CStr(RecordSet("SpouseObjectID")))
  345.     End If
  346.  
  347.     Set ObjectInitializeFromRecordSet = Me
  348. End Function
  349.  
  350.  
  351. Public Function ObjectInitializeRecordSet(Optional RecordSet As Variant) As Long
  352. ' Populate the RecordSet with my variables.
  353. '   Do not initialize the ObjectID column.
  354. '   Return any error code encountered.
  355. '   (in support of VBOFCollection)
  356.  
  357.     On Local Error GoTo InitializeRecordSet_SetError
  358.     Err = 0
  359.     
  360.     RecordSet("CustomerNumber") = CustomerNumber
  361.     RecordSet("FirstName") = FirstName
  362.     RecordSet("LastName") = LastName
  363.     RecordSet("SSN") = SSN
  364.     RecordSet("Sex") = Sex
  365.     If DateOfBirth = #1/1/01# Then
  366.         RecordSet("DateOfBirth") = Null
  367.     Else
  368.         RecordSet("DateOfBirth") = Format$(DateOfBirth, "mm/dd/yyyy")
  369.     End If
  370.     If DateOfDeath = #1/1/01# Then
  371.         RecordSet("DateOfDeath") = Null
  372.     Else
  373.         RecordSet("DateOfDeath") = Format$(DateOfDeath, "mm/dd/yyyy")
  374.     End If
  375.     RecordSet("MaritalStatus") = MaritalStatus
  376.     RecordSet("MaidenName") = MaidenName
  377.     
  378. ' set the Mother object
  379.     If Not pvtMother Is Nothing Then
  380.         RecordSet("MotherObjectID") = _
  381.             pvtMother.ObjectID
  382.     Else
  383.         RecordSet("MotherObjectID") = Null
  384.     End If
  385.     
  386. ' set the Father object
  387.     If Not pvtFather Is Nothing Then
  388.         RecordSet("FatherObjectID") = _
  389.             pvtFather.ObjectID
  390.     Else
  391.         RecordSet("FatherObjectID") = Null
  392.     End If
  393.     
  394. ' set the Spouse object
  395.     If Not pvtSpouse Is Nothing Then
  396.         RecordSet("SpouseObjectID") = _
  397.             pvtSpouse.ObjectID
  398.     Else
  399.         RecordSet("SpouseObjectID") = Null
  400.     End If
  401.         
  402. ' Note:  Do not initialize the ObjectID column.
  403.  
  404.     GoTo InitializeRecordSet_SetError
  405.  
  406. InitializeRecordSet_SetError:
  407.     ObjectInitializeRecordSet = Err
  408.     Exit Function
  409. End Function
  410.  
  411. Public Function ObjectDataSource() As String
  412. ' Return the Data Source with which this Class is associated
  413. '   (in support of VBOFCollection)
  414.     
  415.     ObjectDataSource = "Persons"
  416. End Function
  417. Public Function ObjectNewInstanceOfMyClass() As Person
  418. ' Return a new instance of this class
  419. '   (in support of VBOFCollection)
  420.  
  421.     Set ObjectNewInstanceOfMyClass = New Person
  422. End Function
  423.  
  424.  
  425. Public Function ObjectEventCallBack(Optional Event As Variant, Optional Object As Variant) As Long
  426. ' Receive the Trigger notification and process
  427. '   accordingly
  428. '
  429. ' Parameters:
  430. '   Event
  431. '       a string which identifies the Event
  432. '       Example: "Changed", "Created", "Deleted"
  433. '   Object
  434. '       the object originating the Event.
  435. '       responds to:
  436. '           TypeName(TriggerObject)
  437. '           TriggerObject.ObjectID
  438. ' (supported by VBOFEventManager)
  439.  
  440. End Function
  441.  
  442.  
  443.  
  444.  
  445. Public Function AddAddress(Optional Item As Variant, Optional Parent As Variant) As Address
  446. ' Return an Address, added to my pvtAddressesCollection
  447. '   (just a wrapper method for Addresses.Add)
  448.  
  449.     Set AddAddress = Me.Addresses.Add( _
  450.         Item:=Item, _
  451.         Parent:=Me)
  452.  
  453.     ObjectHasChanged
  454. End Function
  455.  
  456.  
  457. Public Function Addresses(Optional ObjectID As Variant) As Variant
  458. ' Returns a VBOFCollection of Address objects which are
  459. '   contained by this Person object,
  460. ' or
  461. ' Returns an Address object whose ObjectID matches the ObjectID
  462. '   parameter.
  463.  
  464.     Dim tempNewAddress As New Address
  465.  
  466.     Set Addresses = _
  467.         ObjectManager. _
  468.             ManageCollection( _
  469.                 ObjectID:=ObjectID, _
  470.                 Collection:=pvtAddressesCollection, _
  471.                 Parent:=Me, _
  472.                 Sample:=tempNewAddress)
  473.  
  474. End Function
  475.  
  476. Public Function Phones(Optional ObjectID As Variant) As Variant
  477. ' Returns a VBOFCollection of Phone objects which are
  478. '   contained by this Person object,
  479. ' or
  480. ' Returns a Phone object whose ObjectID matches the ObjectID
  481. '   parameter.
  482.  
  483.     Dim tempNewPhone As New Phone
  484.     
  485.     Set Phones = _
  486.         ObjectManager. _
  487.             ManageCollection( _
  488.                 ObjectID:=ObjectID, _
  489.                 Collection:=pvtPhonesCollection, _
  490.                 Parent:=Me, _
  491.                 Sample:=tempNewPhone)
  492.  
  493. End Function
  494.  
  495.  
  496. Public Function FormattedName() As String
  497.  
  498.     Dim ReturnString As String
  499.     
  500.     If UCase$(Left$(Sex, 1)) = "M" Then
  501.         ReturnString = "Mr. "
  502.     ElseIf UCase$(Left$(Sex, 1)) = "F" Then
  503.         ReturnString = "Ms. "
  504.     End If
  505.     
  506.     If UCase$(Left$(Sex, 1)) = "F" _
  507.     And UCase$(Left$(MaritalStatus, 1)) = "M" _
  508.     Then
  509.         ReturnString = "Mrs. "
  510.     End If
  511.     
  512.     ReturnString = ReturnString & FirstName
  513.     
  514.     If MaidenName <> "" Then
  515.         ReturnString = ReturnString & " (" & MaidenName & ")"
  516.     End If
  517.     
  518.     FormattedName = ReturnString & " " & LastName
  519. End Function
  520.  
  521.  
  522. Private Sub Class_Initialize()
  523.  
  524.     CustomerNumber = -1
  525.     FirstName = ""
  526.     LastName = ""
  527.     SSN = -1
  528.     DateOfBirth = #1/1/01#
  529.     DateOfDeath = #1/1/01#
  530.     ObjectID = -1
  531.     
  532.     Set ObjectManager = Nothing
  533.     Set pvtMother = Nothing
  534.     Set pvtFather = Nothing
  535.     Set pvtSpouse = Nothing
  536. End Sub
  537.  
  538.  
  539.  
  540. Public Function Age() As Integer
  541.     
  542.     Dim tempDateOfDeath As Variant
  543.     
  544.     If DateOfBirth = "" _
  545.     Or DateOfBirth = #1/1/01# _
  546.     Then
  547.         Age = 0
  548.         Exit Function
  549.     End If
  550.     
  551.     If DateOfDeath = "" _
  552.     Or DateOfDeath = #1/1/01# _
  553.     Then
  554.         tempDateOfDeath = Now
  555.     Else
  556.         tempDateOfDeath = DateOfDeath
  557.     End If
  558.     
  559.     Age = DateDiff("yyyy", DateOfBirth, tempDateOfDeath)
  560. End Function
  561.  
  562.  
  563. Public Property Get CustomerNumber() As Long
  564.     CustomerNumber = pvtCustomerNumber
  565. End Property
  566.  
  567. Public Property Let CustomerNumber(aLong As Long)
  568.     pvtCustomerNumber = aLong
  569. '    ObjectHasChanged
  570. End Property
  571.  
  572. Public Property Get FirstName() As String
  573.     FirstName = pvtFirstName
  574. End Property
  575.  
  576. Public Property Let FirstName(aString As String)
  577.     pvtFirstName = aString
  578. '    ObjectHasChanged
  579. End Property
  580.  
  581. Public Property Get LastName() As String
  582.     LastName = pvtLastName
  583. End Property
  584.  
  585. Public Property Get MaidenName() As String
  586.     MaidenName = pvtMaidenName
  587. End Property
  588.  
  589.  
  590. Public Property Let LastName(aString As String)
  591.     pvtLastName = aString
  592. '    ObjectHasChanged
  593. End Property
  594.  
  595. Public Property Let MaidenName(aString As String)
  596.     pvtMaidenName = aString
  597. '    ObjectHasChanged
  598. End Property
  599.  
  600.  
  601. Public Property Get SSN() As Long
  602.     SSN = pvtSSN
  603. End Property
  604.  
  605. Public Property Let SSN(aLong As Long)
  606.     pvtSSN = aLong
  607. '    ObjectHasChanged
  608. End Property
  609.  
  610. Public Property Get Sex() As String
  611.     Sex = pvtSex
  612. End Property
  613.  
  614. Public Property Let Sex(aString As String)
  615.     pvtSex = aString
  616. '    ObjectHasChanged
  617. End Property
  618.  
  619. Public Property Get DateOfBirth() As Variant
  620.     DateOfBirth = pvtDateOfBirth
  621. End Property
  622.  
  623. Public Property Get DateOfDeath() As Variant
  624.     DateOfDeath = pvtDateOfDeath
  625. End Property
  626.  
  627.  
  628. Public Property Let DateOfBirth(aDate As Variant)
  629.     pvtDateOfBirth = aDate
  630. '    ObjectHasChanged
  631. End Property
  632.  
  633. Public Property Let DateOfDeath(aDate As Variant)
  634.     pvtDateOfDeath = aDate
  635. '    ObjectHasChanged
  636. End Property
  637.  
  638.  
  639. Public Property Get MaritalStatus() As String
  640.     MaritalStatus = pvtMaritalStatus
  641. End Property
  642.  
  643. Public Property Let MaritalStatus(aString As String)
  644.     pvtMaritalStatus = aString
  645. '    ObjectHasChanged
  646. End Property
  647.